# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： marketing_page.py
# @Time：2023/7/24 14:15
# @Author：lizhixiong
import re
from time import sleep

import allure
from selenium.webdriver.common.by import By

from common.commin.commin import object_send_keys, object_left_click, action_delete, get_ele_text
from common.log.log import log
from data.names import MarketingSel, PublicSel
from po.jinhua.ht.jinhua_ht_main.jinhua_base import JHBase
from po.jinhua.ht.jinhua_ht_main.jinhua_ht_main_page import JHHTHomePage


# 营销菜单下面的所有页面
class MarketingPage(JHBase):
    """
    营销菜单下面的所有页面
    """

    @allure.step('添加优惠券')
    def add_coupon(self, coupon_name, mode='close'):
        day = 30  # 有效期
        amount = 1  # 可领取数量
        try:
            log.info('*****添加优惠券操作*****')
            object_left_click(self.driver, MarketingSel.el_add_coupon())  # 点击添加优惠券按钮
            object_send_keys(self.driver, MarketingSel.el_input_coupon(), coupon_name)  # 输入优惠券名称
            object_send_keys(self.driver, MarketingSel.el_input_period_of_validity(), day)  # 使用有效期限（天）
            object_send_keys(self.driver, MarketingSel.el_input_amount(), amount)  # 可领取数量
            if mode == 'close':
                object_left_click(self.driver, MarketingSel.el_closed_state())  # 点击关闭状态
            elif mode == 'open':
                object_left_click(self.driver, MarketingSel.el_open_state())  # 点击开启状态
            object_left_click(self.driver, MarketingSel.el_gift_voucher())  # 点击领取方式：赠送券
            object_left_click(self.driver, MarketingSel.el_create_now())  # 点击立即创建
        except Exception as e:
            log.error(f'添加优惠券出现错误，异常为{e}')
            raise e

    @allure.step('搜索优惠券')
    def search_coupon(self, coupon_name):
        try:
            log.info('*****搜索优惠券*****')
            object_left_click(self.driver, MarketingSel.el_status_dropdown_box())  # 点击状态下拉
            object_left_click(self.driver, MarketingSel.el_unopened())  # 选择“未开启”
            object_send_keys(self.driver, MarketingSel.el_search_coupon_input(), coupon_name)  # 输入优惠券名称
            object_left_click(self.driver, PublicSel.el_icon_search())  # 点击搜索按钮
            sleep(0.5)
            # if assert_list_total_text_exist(self.driver):
            #     return True
            # else:
            #     return False
        except Exception as e:
            log.error(f'搜索优惠券出现错误，异常为{e}')
            raise e

    @allure.step('删除优惠券')
    def delete_coupon(self, coupon_name):
        try:
            log.info('*****删除优惠券操作*****')
            self.search_coupon(coupon_name)
            action_delete(self.driver)
            sleep(0.5)
        except Exception as e:
            log.error(f'删除优惠券出现错误，异常为{e}')
            raise e

    @allure.step('搜索未使用的指定名称的优惠券')
    def search_not_used_coupon(self, coupon_name='自动化测试永久存在勿删'):
        try:
            log.info('*****搜索未使用的指定名称的优惠券*****')
            JHHTHomePage(self.driver).open_menu('yg', '领取记录')
            object_left_click(self.driver, MarketingSel.el_lq_01())  # 点击使用状态
            object_left_click(self.driver, MarketingSel.el_lq_02())  # 选择未使用
            object_send_keys(self.driver, MarketingSel.el_lq_03(), coupon_name)  # 输入优惠券名称（固定）
            object_left_click(self.driver, MarketingSel.el_lq_04())  # 点击搜索按钮
            sleep(1)
        except Exception as e:
            log.error(f'搜索未使用的指定名称的优惠券，异常为{e}')
            raise e

    @allure.step('立即失效优惠券')
    def immediately_expire_coupon(self, coupon_name='自动化测试永久存在勿删'):
        try:
            log.info('*****立即失效优惠券*****')
            self.search_not_used_coupon(coupon_name)
            object_left_click(self.driver, MarketingSel.el_lq_06())  # 点击立即失效
            object_left_click(self.driver, MarketingSel.el_lq_08())  # 点击二次确定按钮
        except Exception as e:
            log.error(f'立即失效优惠券，异常为{e}')
            raise e

    @allure.step('批量失效优惠券')
    def immediately_expire_coupons(self, coupon_name='自动化测试永久存在勿删'):
        try:
            log.info('*****批量失效优惠券*****')
            self.search_not_used_coupon(coupon_name)
            object_left_click(self.driver, MarketingSel.el_lq_07())  # 点击复选框
            object_left_click(self.driver, MarketingSel.el_lq_05())  # 点击批量立即失效按钮
            object_left_click(self.driver, MarketingSel.el_lq_08())  # 点击二次确认按钮
        except Exception as e:
            log.error(f'批量失效优惠券，异常为{e}')
            raise e



if __name__ == '__main__':
    ele = MarketingSel.el_add_coupon()
    print(ele)
